﻿namespace Oddo.Concept.Crm.Client.Repository
{
    using Microsoft.Practices.Prism.Modularity;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.Prism.UnityExtensions;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.InterceptionExtension;
    using Oddo.Concept.Crm.Client.Repository.ViewModels;
    using Oddo.Concept.Crm.Client.Repository.Views;
    using Oddo.Concept.Crm.Common.Attributes;
    using Oddo.Concept.Crm.Common.Behaviors;
    using Oddo.Concept.Crm.Common.Communication.Interfaces;
    using Oddo.Concept.Crm.Common.Modules;
    using Oddo.Concept.Crm.Common.Presentation.Constants;
    using Oddo.Concept.Crm.Common.Presentation.Infrastructure;
    using Oddo.Concept.Crm.Common.Presentation.Services;
    using Oddo.Concept.Crm.Models;
    using Oddo.Concept.Crm.Repository.Common.Contracts;
    using DelegateCommand = Microsoft.Practices.Prism.Commands.DelegateCommand;

    [Module(ModuleName = "Repository", OnDemand = true)]
    [ModuleAccess(new string[] { "Administrator" })]
    public class RepositoryModule : ClientModuleBase
    {
        private IRegionManager _regionManager;

        public override void RegisterTypes(IUnityContainer container)
        {
            container.RegisterType<IRepositoryServices, RepositoryServicesClient>(
                new Interceptor<InterfaceInterceptor>(), 
                new InterceptionBehavior<MethodCallTraceBehavior>(),
                new InjectionConstructor(new object[] { "https://localhost:4443/", "api/repository/" }));

            container.RegisterType<ICountrySearchView, CountrySearchView>();
            container.RegisterType<CountryViewModel>();
            container.RegisterType<IFindRequest<Country>, CountryFindRequest>();
        }

        public override void RegisterRegions(IRegionManager regionManager)
        {
            _regionManager = regionManager;
            regionManager.RegisterViewWithRegion(Regions.NAVIGATION_REGION, typeof(NavigationGroupView));
            //regionManager.RegisterViewWithRegion(Regions.TABGROUP_REGION, typeof (CountrySearchView));
        }

        public override void ExtendUserInterface()
        {
            var menuService = Container.TryResolve<IMenuService>();
            menuService.AddMainMenuItem(
               new MenuItem("Repository", "Countries", new DelegateCommand(ShowRepositorySearchViewCommand), null) { BeginGroup = true });
        }

        private void ShowRepositorySearchViewCommand()
        {
            _regionManager.AddToRegion(Regions.TABGROUP_REGION, Container.TryResolve<CountrySearchView>());
        }
    }
}